/*
 * fields.h
 *
 *  Created on: Feb 10, 2012
 *      Author: nickolaev
 */

#ifndef FIELDS_H_
#define FIELDS_H_

/*
 * Field names
 */
#define FLD_METHOD              "method"
#define FLD_SEQ                 "seq"
#define FLD_USERNAME            "username"
#define FLD_DIGEST              "digest"

#define FLD_HTSPVERSION         "htspversion"
#define FLD_CLIENTNAME          "clientname"
#define FLD_CLIENTVERSION       "clientversion"


/*
 * RPC method generation
 */
#define HTSMSG_NEW_MGS(fld_num, method_name)  ({\
        HTSMSG* msg = htsmsgNewMessage(fld_num);\
        HTSMSG_CLIENT_METHOD(msg, method_name);\
        msg;\
})
#define HTSMSG_CLIENT_S64(msg, name, value, size_value, pos) {\
        HTSMSG_FIELD* field = htsmsgNewField(HTSMSG_FIELD_S64,\
                        name, size_value,\
                        (void*)(&value));\
       msg->fields[pos] = field;\
}
#define HTSMSG_CLIENT_STR(msg, name, value, pos) {\
        HTSMSG_FIELD* field = htsmsgNewField(HTSMSG_FIELD_STR,\
                        name, strlen(value),\
                        (void*)(value));\
       msg->fields[pos] = field;\
}
#define HTSMSG_CLIENT_BIN(msg, name, value, size_value, pos) {\
        HTSMSG_FIELD* field = htsmsgNewField(HTSMSG_FIELD_BIN,\
                        name, size_value,\
                        (void*)(value));\
       msg->fields[pos] = field;\
}
/*
 * CLIENT
 * all client RPC requests start with filling these fields
 */
#define HTSMSG_CLIENT_METHOD(msg, v) \
        HTSMSG_CLIENT_STR(msg, FLD_METHOD, v, HTSMSG_CLIENT_METHOD_POS)

#define HTSMSG_CLIENT_SEQ(msg, v) {\
        uint8 value = (uint8)(v&0xff);\
        HTSMSG_CLIENT_S64(msg, FLD_SEQ, \
                        value, 1, HTSMSG_CLIENT_SEQ_POS) \
        }

#define HTSMSG_CLIENT_USERNAME(msg, v) \
        HTSMSG_CLIENT_STR(msg, FLD_USERNAME, v, HTSMSG_CLIENT_USERNAME_POS)

#define HTSMSG_CLIENT_DIGEST(msg, v, len) \
        HTSMSG_CLIENT_BIN(msg, FLD_DIGEST, v, len, HTSMSG_CLIENT_DIGEST_POS)

#define HTSMSG_CLIENT_METHOD_POS        (0)
#define HTSMSG_CLIENT_SEQ_POS           (1)
#define HTSMSG_CLIENT_USERNAME_POS      (2)
#define HTSMSG_CLIENT_DIGEST_POS        (3)
/* all RPC specific fields will start from this position */
#define HTSMSG_CLIENT_FLD_POS           (4)

/* HELLO */
#define HTSMSG_HELLO() \
        HTSMSG_NEW_MGS(HTSMSG_HELLO_FLD_NUM, "hello")

#define HTSMSG_HELLO_HTSPVERSION(msg, v) \
        {\
        uint8 value = (uint8)(v&0xff);\
        HTSMSG_CLIENT_S64(msg, FLD_HTSPVERSION, \
                          value, 1, HTSMSG_HELLO_HTSPVERSION_POS) \
        }

#define HTSMSG_HELLO_CLIENTNAME(msg, v) \
        HTSMSG_CLIENT_STR(msg, FLD_CLIENTNAME, v, HTSMSG_HELLO_CLIENTNAME_POS)

#define HTSMSG_HELLO_CLIENTVERSION(msg, v) \
        HTSMSG_CLIENT_STR(msg, FLD_CLIENTVERSION, v, HTSMSG_HELLO_CLIENTVERSION_POS)

#define HTSMSG_HELLO_HTSPVERSION_POS        (HTSMSG_CLIENT_FLD_POS + 0)
#define HTSMSG_HELLO_CLIENTNAME_POS         (HTSMSG_CLIENT_FLD_POS + 1)
#define HTSMSG_HELLO_CLIENTVERSION_POS      (HTSMSG_CLIENT_FLD_POS + 2)

#define HTSMSG_HELLO_FLD_NUM                (HTSMSG_CLIENT_FLD_POS + 1)


#endif /* FIELDS_H_ */
